﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Activity
{
    public class DetailsActivityModel
    {
        public int ActivityId { get; set; }
        public string Name { get; set; }
        public string Management { get; set; }
        public string Process { get; set; }
        public string Area { get; set; }

        private riskanalysisEntities entities = new riskanalysisEntities();

        public DetailsActivityModel(int activityId)
        {
            var query = from a in entities.activities
                        where a.activityId == activityId && a.enabled
                        select new
                        {
                            ActivityId = a.activityId,
                            Name = a.name,
                            Management = a.area.process.management.name,
                            Process = a.area.process.name,
                            Area = a.area.name
                        };

            var theActivity = query.FirstOrDefault();

            if (theActivity == null)
                throw new DetailsActivityException("La actividad especificada no existe");

            ActivityId = theActivity.ActivityId;
            Name = theActivity.Name;
            Management = theActivity.Management;
            Process = theActivity.Process;
            Area = theActivity.Name;
        }

        public void DeleteManagement()
        {
            var managementList = from a in entities.activities
                                 where a.activityId == ActivityId && a.enabled
                                 select a;

            if (managementList.Count() <= 0)
                throw new DetailsActivityException("La actividad especificada no fue encontrada");

            managementList.First().enabled = false;
            entities.SaveChanges();
        }
    }
}